document.addEventListener('deviceready',init,false);
function init(){
	alert("innit");
	showFriend();
	showFriendCon()
	//朋友圈和通讯录切换
	$('#txl').on('singleTap',function(){
		$('.friendsCircle').hide();
		$('.friendList').show();
		$(this).css("color","#3C7A03").siblings().css("color","#8A8A8A");
	});
	
	$('#fx').on('singleTap',function(){
		$('.friendsCircle').show();
		$('.friendList').hide();
		$(this).css("color","#3C7A03").siblings().css("color","#8A8A8A");	
		//显示朋友圈
		showFriendCon();
	});
	//点击添加好友
	$('#addF').on('singleTap',function(){
		$('#tjFriend').show().siblings().hide();
		$('#tjFriend .searchfriends').val('添加新好友')
		$('#tjFriend .searchCon').html('');
		$('#tjFriend .searchCancel').on('singleTap',function(){
			$('.friendList').show();
			$('.footer').show();
			$('#tjFriend').hide();
		});
		$('#tjFriend .okBtn').on('singleTap',function(){
			$('#tjFriend .searchCon').html('');
			var valu = $('#tjFriend .searchfriends').val();		
			findAllUser(valu);
			$('#tjFriend .searchfriends').val('添加新好友');
		});	
	});
	
	//点击搜索好友
	$('#findF').on('singleTap',function(){
		$('#ssFriend').show().siblings().hide();
		$('#ssFriend .searchCancel').on('singleTap',function(){
			$('.friendList').show();
			$('.footer').show();
			$('#ssFriend').hide();
		});
		$('#ssFriend .okBtn').on('singleTap',function(){
			$('#ssFriend .searchCon').html('');
			var valu = $('#ssFriend .searchfriends').val();
	        valu = valu?valu:'';
			alreadyFriend(valu);
			//val为空代表获取全部
			$('#ssFriend .searchfriends').val('搜索已有好友');
		});
	});
	
	//显示朋友的朋友圈
	function showFriendCon(){
		$('.conMain').html('');
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/list.php",
			async:true,
			data: { userId: 'huihui' },
			dataType:"jsonp",
			success:function(data){
				var len = data.data.length;
				for(var j=0;j<len;j++){
					 var str = '<section><div class="seHead"><a href="#"><img src="'+data.data[j].header+'"/></a></div><div class="seText"><div class="userI"><span class="usName">'+data.data[j].name+'</span></div><div class="fon">'+ data.data[j].text +'</div>';
					 var lenK = data.data[j].imgs.length;
					 if(lenK>0){
					 	str +='<div class="pics">';
					 	for(var k=0;k<lenK;k++){
					 		str+='<div><a href="#"><img src="'+data.data[j].imgs[k]+'"/></a></div>';
					 	}
					 	str += '</div>';
					 }
					 str += '<div class="time"><span>'+data.data[j].time+'</span><span class="iconfont like" >&#xe605;</span></div></div></section>';
					 $('.conMain').append(str);
				}
			}		
		});
	}
	
	//查找用户API
	function findAllUser(fId){
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/searchfriends.php",
			async:true,
			data: { userId: 'huihui',friendId:fId},
			dataType:"jsonp",
			success:function(data){
				$('#tjFriend .searchfriends').val('添加新好友');
				if(data.status==1){
					var len = data.friends.length;
					if(len==0){
						alert("用户不存在")
					}else if(len>0){
						for(var i=0;i<len;i++){
							var str ='<div class="friend"><a href="#" class="friendhead"><img src="'+ data.friends[i].header +'"/></a><span class="friendname">'+data.friends[i].friendsId+'</span><span class="addit">添加</span></div>';
							 $('#tjFriend .searchCon').append(str);						 
						}
						$("#tjFriend .addit").on('singleTap',function(){
							var htm = $(this).siblings('.friendname').html();
							var ele = $(this);
							addFriend(htm,ele);						
						});
					}
				}else{
					alert('查找出错！');
				}
			}
		});
	}
	////判断该人是否是你好友
	//function judgeFriend(){
	//	var tep;
	//	$.ajax({
	//		type:"get",
	//		url:"http://10.9.170.108/friends.php",
	//		async:true,
	//		data: { userId: 'huihui' },
	//		dataType:"jsonp",
	//		success:function(data){
	//			tep = data.friends;
	//		}
	//	});
	//	return tep;
	//}
	
	//添加好友
	function addFriend(name,ele){
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/addfriends.php",
			async:true,
			data: { userId: 'huihui', friendId:name},
			dataType:"jsonp",
			success:function(data){
				console.log(data)
				if(data.status==1){
					alert('添加成功');
					ele.css({
						background:"#999"
					})
					ele.html("已添加")
				}else if(data.status==0){
					alert(data.message)
				}
			}
		});
	}
	//搜索已有好友
	function alreadyFriend(valu){	
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/friends.php",
			async:true,
			data: { userId: 'huihui' },
			dataType:"jsonp",
			success:function(data){
				var len = data.friends.length;
				if(valu!=''){
					var userF;
					for(var i=0;i<len;i++){
						if(data.friends[i].friendsId==valu){
							userF = data.friends[i];
							break;
						}
					}
					if(userF){
						var str = '<div class="friend"><a href="#" class="friendhead"><img src="'+userF.header+'"/></a><span class="friendname">'+userF.friendsId+'</span></div>';
						$("#ssFriend .searchCon").append(str);
					}else{
						alert('您没有添加此好友！')
					}
				}else{
					for(var j=0;j<len;j++){
						var stra = '<div class="friend"><a href="#" class="friendhead"><img src="'+data.friends[j].header+'"/></a><span class="friendname">'+data.friends[j].friendsId+'</span></div>';
						$("#ssFriend .searchCon").append(stra);
					}
				}
			}
		});
	}
	//取得好友
	function showFriend(){
		$('#txl').css("color","#3C7A03").siblings().css("color","#8A8A8A");
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/friends.php",
			async:true,
			data: { userId: 'huihui' },
			dataType:"jsonp",
			success:function(data){
				if(data.status==1){
					$('.friendList .mainList').html('');
					for(var i=0;i<data.friends.length;i++){
						var str ='<div class="friend"><a href="#" class="friendhead"><img src="'+ data.friends[i].header +'"/></a><span class="friendname">'+data.friends[i].friendsId+'</span><span class="deleleit">删除</span></div>';
						 $('.mainList').append(str);
					}
					$('.friendList .mainList .friend').on('swipeLeft',function(){
						$(this).find('.deleleit').show().on("singleTap",function(){
							var htm = $(this).siblings('.friendname').html();
							var r = confirm("确定要删除好友吗？")
							if(r==true){
								deleteFriend(htm);
								showFriend();
							}else{
								showFriend();
							}						
						});
					});
					$('.friendList .mainList .friend').on('swipeRight',function(){
						$(this).find('.deleleit').hide();
					});
				}
			}
		});
	}
	//删除好友
	function deleteFriend(fId){
		$.ajax({
			type:"get",
			url:"http://10.9.170.108/delfriends.php",
			async:true,
			data: { userId: 'huihui', friendId:fId},
			dataType:"jsonp",
			success:function(data){
				alert('删除成功！')
			}
		});
	}

	
	//从相册选取文件上传
	//发布朋友圈
	var flag = true;
	$('#photo').on('singleTap',function(){
		if(flag){
			$('#publish').show();
			document.addEventListener("backbutton", yourCallbackFunction, false);
			function yourCallbackFunction(){
				$('#publish').hide();
			}
		}else{
			$('#publish').hide();			
		}
		flag = !flag;
	});
	//发朋友圈
	function publishCon(imageData){	
		$("#fb").show().siblings().hide();
		var ele = document.getElementById('imgpic');
		ele.src = imageData;
		//点击发送
		$('#fb #sendA').on("singleTap",function(){
			var sendCon  = $('#sendText').val();
			var win = function(r) { 	        				
				alert("发送成功！");
				fanhui();
			}  			
			var fail = function(error) { 
				alert("发送失败！")
			}			
			var options = new FileUploadOptions(); 
			options.fileKey="file"; 
			options.fileName=imageData.substr(imageData.lastIndexOf('/')+1); 
			options.mimeType="text/plain";  			
			var params = new Object(); 
			params.userId = "huihui"; 
			params.text = sendCon;  			
			options.params = params;  			
			var ft = new FileTransfer(); 
			ft.upload(imageData, "http://10.9.170.108/upload.php", win, fail, options);
		});
		//点击返回
		$('#fb #fh').on("singleTap",function(){
			fanhui();
		})
		function fanhui(){
			$("#fb").hide();
			$('.friendsCircle').show();
			$('.footer').show();
			$('#fx').css("color","#3C7A03").siblings().css("color","#8A8A8A");	
			//显示朋友圈
			showFriendCon();
		}
	}
	document.getElementById("xiangce").addEventListener("touchend",function(){
		var option = {
			quality: 50,
			destinationType : Camera.DestinationType.FILE_URI,
			sourceType : 0
		};
		navigator.camera.getPicture(onSuccess, onFail, option);
		function onSuccess(imageData) {
			//alert(imageData);
			publishCon(imageData)
		}
		function onFail(message) {
		   alert('Failed because: ' + message);
		}
	});
	document.getElementById("paizhao").addEventListener("touchend",function(){
		var option = {
			quality: 50,
			destinationType : Camera.DestinationType.FILE_URI,
			sourceType : 1
		};
		navigator.camera.getPicture(onSuccess, onFail, option);
		function onSuccess(imageData) {

			publishCon(imageData)
		}
		function onFail(message) {
		   alert('Failed because: ' + message);
		}
	})

}


